package com.cjr.ddp.controller;

import com.cjr.ddp.entity.User;
import com.cjr.ddp.service.UserService;
import com.cjr.ddp.util.ReturnMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author cjr
 * @date Created in 2020-03-07 22:04
 * @description 用户控制层
 * @modified By
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    /**
     * @param user
     * @return com.cjr.ddp.util.ReturnMsg
     * @throws
     * @author cjr
     * @date 2020-03-07 22:15
     * @description 用户新增
     */
    @RequestMapping("/insertUser")
    public ReturnMsg insertUser(User user) {
        ReturnMsg returnMsg = new ReturnMsg();
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        List<User> userList = userService.selectUserByUserName(user);
        if (userList != null && userList.size() != 0) {
            returnMsg.setCode("404");
            returnMsg.setSuccess(false);
            returnMsg.setMsg("该用户名已经存在，请更改!");
        } else {
            int insertBack = userService.insertUser(user);
            if (insertBack > 0) {
                returnMsg.setCode("200");
                returnMsg.setSuccess(true);
                returnMsg.setMsg("新增用户成功!");
            } else {
                returnMsg.setCode("404");
                returnMsg.setSuccess(false);
                returnMsg.setMsg("新增用户失败，请重试!");
            }
        }

        return returnMsg;

    }

    /**
     * @param user
     * @return com.cjr.ddp.util.ReturnMsg
     * @throws
     * @author cjr
     * @date 2020-03-07 22:29
     * @description 用户删除
     */
    @RequestMapping("/deleteUserByUserID")
    public ReturnMsg deleteUserByUserID(User user) {

        ReturnMsg returnMsg = new ReturnMsg();

        int deleteBack = userService.deleteUserByUserId(user.getId());
        if (deleteBack > 0) {
            returnMsg.setCode("200");
            returnMsg.setSuccess(true);
            returnMsg.setMsg("删除成功!");
        } else {
            returnMsg.setCode("404");
            returnMsg.setSuccess(false);
            returnMsg.setMsg("删除失败，请重试!");
        }
        return returnMsg;
    }


    /**
     * @param user
     * @return com.cjr.ddp.util.ReturnMsg
     * @throws
     * @author cjr
     * @date 2020-03-07 22:29
     * @description 用户修改
     */
    @RequestMapping("/updateUserByUserID")
    public ReturnMsg updateUserByUserID(User user) {

        ReturnMsg returnMsg = new ReturnMsg();
        if(user.getPassword()!=null&&!user.getPassword().equals("")){
            user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        }


        int updateBack = userService.updateUserByUserID(user);
        if (updateBack > 0) {
            returnMsg.setCode("200");
            returnMsg.setSuccess(true);
            returnMsg.setMsg("修改成功!");
        } else {
            returnMsg.setCode("404");
            returnMsg.setSuccess(false);
            returnMsg.setMsg("修改失败，请重试!");
        }
        return returnMsg;
    }


    /**
     * @param
     * @return java.util.List<com.cjr.ddp.entity.User>
     * @throws
     * @author cjr
     * @date 2020-03-07 22:33
     * @description 查询所有用户
     */
    @RequestMapping("/selectUserList")
    public List<User> selectUserList() {
        List<User> userList = userService.selectUserList();
        return userList;
    }


}
